package com.maduixa.guitar.tabsearch.spring.business;


import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import com.maduixa.guitar.tabsearch.spring.business.model.Song;
import com.maduixa.guitar.tabsearch.spring.business.model.UserSong;
import com.maduixa.guitar.tabsearch.spring.data.DataController;
import com.maduixa.guitar.tabsearch.spring.to.ListSongTO;

public class SongsController{

    private DataController dB;

    public SongsController(DataController dB) {
		super();
		this.dB = dB;
	}
    
    public Song addSong(String artist, String song, String tune, String coments, String video,
    		Integer g1, Integer g2, Integer g3, Integer bass, Integer solo, String tabLink, 
    		String tabName, String songsterr, String summary) {    
    	
    	Song s = new Song(artist, song, tune, coments, video, g1,g2,g3,bass,solo,tabLink,tabName, songsterr, summary);
        dB.addSong(s);
        
        return s;
    }
    
    public void updateSong(long id, String artist, String song, String tune,
			String coments, String video, Integer guitar1, Integer guitar2,
			Integer guitar3, Integer bass, Integer solo, String tabLink,
			String tabName, String songsterr, String summary) {
		
		Song s = dB.getSong(id);
		s.setArtist(artist);
		s.setSong(song);
		s.setTune(tune);
		s.setComents(coments);
		s.setVideo(video);
		s.setGuitar1(guitar1);
		s.setGuitar2(guitar2);
		s.setGuitar3(guitar3);
		s.setBass(bass);
		s.setSolo(solo);
		s.setTabLink(tabLink);
		s.setTabName(tabName);
		s.setSongsterr(songsterr);
		s.setSummary(summary);
		
		dB.updateSong(s);
	}
    
	public void removeSong(long id) {
		Song s = dB.getSong(id);
		dB.removeSong(s);
	}
	
	public boolean checkSongUsage(long id) {
		Song s = dB.getSong(id);
		List<UserSong> users = dB.getAllUserSong(s);
		if (users == null || users.size() < 1)
			return false;
		else
			return true;
	}
    
	public Song getSong(long id) {
		return dB.getSong(id);			
	}

    public List<ListSongTO> getAllSongs(String userId) {
        List<ListSongTO> songs = new ArrayList<ListSongTO>();
        List<Object> select = dB.getAllSongs(userId);
        for (int i = 0; i < select.size(); i++) {
        	Object[] o = (Object[])select.get(i);
        	ListSongTO lsto = new ListSongTO(((BigInteger)o[0]).longValue(), (String) o[1],(String)o[2],(String)o[3],(Integer)o[4],
        			(Integer)o[5],(Integer)o[6],(Integer)o[7],(Integer)o[8],(String)o[9]);
        	songs.add(lsto);
		}
        return songs;
    }

	public List<ListSongTO> getSongs(String userId, String artist, String song, String tune, String order, String direction) {
		List<ListSongTO> songs = new ArrayList<ListSongTO>();
		
		if ("".equals(artist)) artist = null;
		if ("".equals(song)) song = null;
		if ("".equals(tune)) tune = null;
        
		List<Object> select = dB.getSongs(userId,artist,song,tune, order, direction);
        
        for (int i = 0; i < select.size(); i++) {
        	Object[] o = (Object[])select.get(i);
        	ListSongTO lsto = new ListSongTO(((BigInteger)o[0]).longValue(), (String) o[1],(String)o[2],(String)o[3],(Integer)o[4],
        			(Integer)o[5],(Integer)o[6],(Integer)o[7],(Integer)o[8],(String)o[9]);
        	songs.add(lsto);
		}
        
        return songs;
	}


}
